/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.ui.async.gui;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.ourgrid.common.interfaces.status.CommunityInfo;
import org.ourgrid.common.interfaces.to.DiscoveryServiceState;

public class CommunityPanel extends javax.swing.JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private CommunityTableModel model;

	private static final ImageIcon UPICON = new ImageIcon(PeerUserTableModel.USER_ONLINE_IMAGE_PATH, "UP");
	
	private static final ImageIcon CONTACTINGICON = new ImageIcon(PeerUserTableModel.USER_OFFLINE_IMAGE_PATH, "CONTACTING");
	
	private JLabel iconLabel;
	
	/** Creates new form CommunityPanel */
    public CommunityPanel() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        dsPanel = new javax.swing.JPanel();
        dsAddressText = new javax.swing.JTextField();
        peersPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        peersTable = new javax.swing.JTable();
        
        iconLabel = new JLabel(CONTACTINGICON, JLabel.LEFT);
        
        dsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("DiscoveryService"));

        dsAddressText.setEditable(false);
        dsAddressText.setEnabled(true);
        
        org.jdesktop.layout.GroupLayout dsPanelLayout = new org.jdesktop.layout.GroupLayout(dsPanel);
        dsPanel.setLayout(dsPanelLayout);
        dsPanelLayout.setHorizontalGroup(
            dsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(dsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(iconLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(dsAddressText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        dsPanelLayout.setVerticalGroup(
            dsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(dsPanelLayout.createSequentialGroup()
                .add(dsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, iconLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, dsAddressText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        peersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Connected peers"));

        this.model = new CommunityTableModel();
        
        peersTable.setModel(model);
        jScrollPane1.setViewportView(peersTable);

        org.jdesktop.layout.GroupLayout peersPanelLayout = new org.jdesktop.layout.GroupLayout(peersPanel);
        peersPanel.setLayout(peersPanelLayout);
        peersPanelLayout.setHorizontalGroup(
            peersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(peersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        peersPanelLayout.setVerticalGroup(
            peersPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(peersPanelLayout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, peersPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, dsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(dsPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(peersPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        
    }// </editor-fold>
    
    
	// Variables declaration - do not modify
    private javax.swing.JTextField dsAddressText;
    private javax.swing.JPanel dsPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable peersTable;
    private javax.swing.JPanel peersPanel;
    // End of variables declaration

	public void setTableModelData(CommunityInfo discoveryServiceInfo) {
		this.model.setData(discoveryServiceInfo);
		this.model.fireTableDataChanged();
		this.dsAddressText.setText(discoveryServiceInfo.getDiscoveryServiceAddress());
	
		setStatusIcon(discoveryServiceInfo);
		
		this.repaint();
	}

	private void setStatusIcon(CommunityInfo discoveryServiceInfo) {
		this.iconLabel.setIcon(discoveryServiceInfo.getDiscoveryServiceStatus().equals(
				DiscoveryServiceState.UP) ? UPICON : CONTACTINGICON);
	}

	public void peerStopped() {
		setTableModelData(new CommunityInfo());		
	}
    
}
